package com.cxc.template;

import com.cxc.entity.Fruite;
import com.cxc.strategy.Discount;

import java.util.ArrayList;
import java.util.List;

/**
 * 模板方法模式
 * 购物车费用结算过程
 */
public abstract class ShoppingCart {
  private Discount discount;
  private List<Fruite> products = new ArrayList<>();

    public void setDiscount(Discount discount) {
        this.discount = discount;
    }

    public ShoppingCart(List<Fruite> products) {
        this.products = products;
    }

    public ShoppingCart(Discount discount) {
        this.discount = discount;
    }



    //提交订单主流程
    public void submitOrder(){
        //1、计算商品名称
        int money = balance();
        System.out.println("商品总金额为："+money+"元");
        //优惠减免
        money = discount.cacluate(money);
        System.out.println("优惠减免后："+ money+"元，");

        //保存订单
        pay(money);
        //送货上门
        sendHome();

    }

    private void sendHome(){
        System.out.println("三公里以内，免费送货上门");
    }

    protected abstract void pay(int money);

    //计算金额
    private int balance(){
        int money = 0;
        System.out.print("商品清单：");
        for (Fruite fruit : products){
            fruit.draw();
            System.out.print(",");
            money += fruit.price();
        }
        return money;
    }


}


